<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Model\User as UserModel;
use App\Controller\BaseController;

use Psr\Log\LoggerInterface;
use Hyperf\Logger\LoggerFactory;

#[AutoController]
class UserController extends BaseController
{
	private $request;

	protected LoggerInterface $logger;

	public function __construct(LoggerFactory $loggerFactory)
	{
		// 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
		$this->logger = $loggerFactory->get('log', 'new');
	}

	/**
	 * 修改用户信息
	 */
	public function userEdit(RequestInterface $request)
	{
		$isAuth = $this->Auth($request, 1);
		$AuthInfo = json_decode($isAuth, true);
		$formData = $request->post();
		if ($AuthInfo['err'] === 0) {
			if (!isset($formData['formdata']['id']) || empty($formData['formdata']['id']) || !isset($formData['formdata']['nickname']) || !is_string($formData['formdata']['nickname'])) {
				$res['err'] = 1;
				$res['msg'] = '修改失败，昵称为空或格式错误';
				return json_encode($res);
			}
			$result = UserModel::userEdit($formData);
			if ($result) {
				$res['err'] = 0;
				$res['msg'] = '修改成功';
			} else {
				$res['err'] = 1;
				$res['msg'] = '修改失败';
			}
		} else {
			$res['err'] = 2;
			$res['msg'] = '认证失败，重新登录';
		}
		return json_encode($res);
	}

	/**
	 * 上传头像
	 */
	public function uploadAvatar(RequestInterface $request)
	{
		$isAuth = $this->Auth($request, 1);
		$AuthInfo = json_decode($isAuth, true);
		if ($AuthInfo['err'] == 0) {
			$userId = $request->input('userid');
			$time = time();
			$imgName = 'avatar/header_' . $userId . '.jpg';
			$imgPath = '/www/wwwroot/fengjian.com/upload/' . $imgName;
			$imgType = ['jpg', 'jpeg', 'png'];
			if (!$userId) {
				$res['err'] = 1;
				$res['msg'] = '用户id不存在';
				$res['imgPath'] = '';
				return $res;
			}
			if ($request->hasFile('file')) {
				if ($request->file('file')->isValid()) {
					$fileInfo = $request->file('file')->toArray();
					if ($fileInfo['size'] > 204800) {
						$res['err'] = 1;
						$res['msg'] = '图片尺寸太大';
						$res['imgPath'] = '';
						return $res;
					}
					$extension = $request->file('file')->getExtension();
					if (!in_array($extension, $imgType)) {
						$res['err'] = 1;
						$res['msg'] = '图片格式不符合';
						$res['imgPath'] = '';
						return $res;
					}
					$request->file('file')->moveTo($imgPath);
					if (!$request->file('file')->isMoved()) {
						$res['err'] = 1;
						$res['msg'] = '图片上传错误';
						$res['imgPath'] = '';
						return $res;
					}
					// 图片转移成功后，改变权限
					chown($imgPath, 'www');
					chgrp($imgPath, 'www');
					chmod($imgPath, 0640);
					// 修改数据库
					$result = UserModel::avatarEdit($userId, $imgName);
					if (!$result) {
						$res['err'] = 1;
						$res['msg'] = '图片修改失败';
						$res['imgPath'] = '';
						return $res;
					}
					$res['err'] = 0;
					$res['msg'] = '图片上传成功';
					$res['imgPath'] = $imgName;
				} else {
					$res['err'] = 1;
					$res['msg'] = '图片上传失败';
					$res['imgPath'] = '';
				}
			} else {
				$res['err'] = 1;
				$res['msg'] = '图片文件不存在';
				$res['imgPath'] = '';
			}
		} else {
			$res['err'] = 2;
			$res['msg'] = '认证失败，重新登录';
			$res['imgPath'] = '';
		}
		return json_encode($res);
	}
}
